package com.siruier.boss.ui.activity.mall

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.kingja.loadsir.core.LoadService
import com.siruier.boss.R
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.mallApi
import com.siruier.boss.bean.LogisticsDetailBean
import com.siruier.boss.bean.LogisticsStateEnum
import com.siruier.boss.bean.ShipRecord
import com.siruier.boss.databinding.ActivityLogisticsBinding
import com.siruier.boss.databinding.ItemLogisticsBinding
import com.siruier.boss.ui.api.ApiObserver
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.base.BaseRecyclerAdapter
import com.siruier.boss.ui.ext.*
import com.siruier.boss.ui.helper.InjectBundle
import com.siruier.boss.ui.helper.loadsir.LoadSirHelper
import com.siruier.boss.ui.helper.loadsir.LoadingCallback

class LogisticsActivity : BaseActivity<ActivityLogisticsBinding>() {
    private lateinit var mLoadService: LoadService<*>
    private var mShipRecord: ShipRecord? = null
    private val orderSn: String by InjectBundle()

    override fun initView() {
        bindViewClick(vb.buttonCopy)
        mLoadService = LoadSirHelper.register(vb.rootView, listener = ::shipRecord)
        shipRecord()
    }

    private fun shipRecord() {
        launchUI({
            mallApi.shipRecord(orderSn).toData()?.let { shipRecord ->
                mShipRecord = shipRecord
                if (!shipRecord.data.isNullOrEmpty()) {
                    val data = shipRecord.data[0]
                    val stateEnum = LogisticsStateEnum.values().find {
                        it.state == data.status
                    } ?: LogisticsStateEnum.STATE_1
                    vb.tvLogisticsStatus.textSpan(getString(R.string.text_logistics_status, stateEnum.stateName)) {
                        spanText = stateEnum.stateName
                        addForegroundColorSpan(R.color.mall_main.toColor())
                    }
                }
                vb.tvLogisticsCompany.text = getString(R.string.text_logistics_company, shipRecord.comName)
                vb.tvLogisticsOrderNo.text = getString(R.string.text_logistics_order_no, shipRecord.nu)
                vb.tvLogisticsTel.text = getString(R.string.text_logistics_tel, shipRecord.tel)
                vb.tvLogisticsUpdateTime.text = getString(R.string.text_logistics_update_time, shipRecord.updateTime)

                shipRecord.data?.let {
                    vb.rvLogistics.adapter = LogisticsAdapter().apply { datas = it }
                    vb.rvLogistics.layoutManager = LinearLayoutManager(mThis)
                }
            }
        }, ApiObserver(loadService = mLoadService))
    }

    override fun onClick(v: View?) {
        mShipRecord?.nu.clipboard()
    }
}

class LogisticsAdapter : BaseRecyclerAdapter<ItemLogisticsBinding, LogisticsDetailBean>() {
    override fun onBindViewHolder(vb: ItemLogisticsBinding, bean: LogisticsDetailBean, position: Int) {
        vb.tvContext.text = bean.context
        vb.tvTime.text = bean.time
        if (0 == position) {
            vb.ivImage.setImageResource(R.mipmap.icon_main_check)
            vb.tvContext.setTextColor(R.color.main_color.toColor())
            vb.tvTime.setTextColor(R.color.main_color.toColor())
        } else {
            vb.ivImage.setImageResource(R.mipmap.icon_logistics_dot)
            vb.tvContext.setTextColor(R.color.text_color.toColor())
            vb.tvTime.setTextColor(R.color.text_hint_color.toColor())
        }
    }
}